package com.etlmaster.executor.utils;

import java.util.UUID;
import java.util.concurrent.Callable;

import com.etlmaster.executor.EtlMasterExecutor;
import com.etlmaster.executor.StepStack.MetaEntry;
import com.etlmaster.executor.StepStack;
import com.etlmaster.executor.steps.control.RingStepMeta;

/**@author Larry
 * @date 2017-04-14
 * 线程实例
 */
public class ThreadEntity implements Callable<CallableResult> {

	private MetaEntry entry;
	private MultiProcessHelper helper;
	private StepStack stack;
	public int FAILURE = 1;
	public static final String SUCCESS_MSG = "success";
	public static final int SUCCESS = 0;

	public ThreadEntity(StepStack stack,MetaEntry entry, MultiProcessHelper helper) {
		this.entry = entry;
		this.helper = helper;
		this.stack=stack;
	}

	public CallableResult call() throws Exception {
		CallableResult result=null;

		try {
			EtlMasterExecutor.runStep(stack,entry);
			
			result= new CallableResult(SUCCESS, SUCCESS_MSG,entry);
			if(entry.getMeta() instanceof RingStepMeta){
				entry.setId(UUID.randomUUID().toString());
			}
		} catch (Exception e) {			
			e.printStackTrace();
			result=new CallableResult(FAILURE, e.getMessage(),entry);
		}
		stack.getLock().lock();
		stack.getNotEmpty().signal();
		entry.getJob().putRunOffStepMetas(entry.getId(),entry.getMeta().getId(), entry.getMeta());
		stack.getResult().add(result);
		stack.getLock().unlock();

		return result;
	}
}
